package com.hzncc.flowable_diboot.rabbitmq.listener;

import com.diboot.core.util.JSON;
import com.diboot.core.util.S;
import com.hzncc.flowable_diboot.rabbitmq.config.Cons;
import com.hzncc.flowable_diboot.rabbitmq.service.impl.SendMessageService;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.Charset;

@Log4j2
@Component
public class RabbitmqDelayMessageListener {

    @Autowired
    private SendMessageService sendMessageService;

    /**
     * 延时消息消费
     *
     * @param msg
     * @author 陈广煜
     * @date 2024/6/21
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = Cons.DELAY_QUEUE, durable = "true"),
            exchange = @Exchange(value = Cons.DELAY_EXCHANGE, delayed = "true"), key = Cons.ROUTING_KEY))
    public void test(Message msg) {
        // 获取延时消息
        String jsonStr = S.toEncodedString(msg.getBody(), Charset.defaultCharset());
        com.diboot.notification.entity.Message record = JSON.parseObject(jsonStr, com.diboot.notification.entity.Message.class);
        log.info("接收延时消息：{}",record.getContent());
        // 发送到mqtt
        sendMessageService.sendRetainedMessage(record);
    }




}
